package com.project.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.bean.EmployeeBean;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("em")
public class EmployeeController {

    //服务器校验先将表单数据完成实体对象数据封装，
    // 然后再对封装在实体对象中的数据进行校验
    @RequestMapping("add")
    public String add(@Valid EmployeeBean em, BindingResult result)throws Exception{
        if(result.hasErrors()){//判断校验是否有错误信息
            //得到校验失败后，错误信息的集合
            List<FieldError> errorList = result.getFieldErrors();
            ObjectMapper om = new ObjectMapper();
            return om.writeValueAsString(errorList);
        }
        System.out.println("执行业务方法");
        return "ok";
    }
}
